using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using PluginHandler.UI;

namespace NpgsqlPlugin.UI
{
    internal partial class ConnectionDialog : UserControl, IConnectionDialog
    {
        private DbConnectionStringBuilder _connectionStringBuilder;

        private const int _defaultPort = 5432;

        public ConnectionDialog()
        {
            InitializeComponent();

            numPort.Value = _defaultPort;
        }

        #region IConnectionDialog Member

        public DbConnectionStringBuilder ConnectionStringDetails
        {
            get 
            {
                if (_connectionStringBuilder == null)
                    _connectionStringBuilder = new DbConnectionStringBuilder();

                _connectionStringBuilder["Server"] = txtServer.Text;
                _connectionStringBuilder["Port"] = numPort.Text;
                _connectionStringBuilder["Database"] = txtDatabase.Text;
                _connectionStringBuilder["User ID"] = txtUsername.Text;
                _connectionStringBuilder["Password"] = txtPassword.Text;

                //Extended settings
                _connectionStringBuilder["Timeout"] = numTimeout.Value.ToString();
                _connectionStringBuilder["Encoding"] = cmbEncoding.SelectedText;
                
                _connectionStringBuilder["SSL"] = chkSSL.Checked ? "true" : "false";
                _connectionStringBuilder["Sslmode"] = cmbSSLMode.SelectedText;
                
                _connectionStringBuilder["Pooling"] = chkPooling.Checked ? "true" : "false";
                _connectionStringBuilder["MinPoolSize"] = numMinPoolSize.Value.ToString();
                _connectionStringBuilder["MaxPoolSize"] = numMinPoolSize.Value.ToString();

                if (cmbSSLMode.SelectedIndex > 0)
                {
                    _connectionStringBuilder["Protocol"] = cmbSSLMode.SelectedText;
                }
                else
                {
                    _connectionStringBuilder.Remove("Protocol");
                }

                return _connectionStringBuilder;
            }
            set 
            {
                _connectionStringBuilder = value;

                int port;

                if (!int.TryParse(_connectionStringBuilder["Port"].ToString(), out port))
                {
                    port = _defaultPort;
                }

                txtServer.Text = TryGetValue(_connectionStringBuilder, "Server");
                txtDatabase.Text = TryGetValue(_connectionStringBuilder, "Database");
                numPort.Value = port;
                txtUsername.Text = TryGetValue(_connectionStringBuilder, "User ID");
                txtPassword.Text = TryGetValue(_connectionStringBuilder, "Password");
            }
        }

        public bool FilledComplete
        {
            get
            {
                bool filledComplete = false;

                filledComplete =
                    txtServer.Text.Trim().Length > 0 &&
                    txtDatabase.Text.Trim().Length > 0 &&
                    txtUsername.Text.Trim().Length > 0;

                return filledComplete;
            }
        }

        public bool AskForPassword
        {
            get { return chkAskForPassword.Checked; }
            set { chkAskForPassword.Checked = value; }
        }
        #endregion

        private void ConnectionDialog_Load(object sender, EventArgs e)
        {
            cmbEncoding.SelectedIndex = 0;
            cmbProtocol.SelectedIndex = 0;
            cmbSSLMode.SelectedIndex = 0;
        }

        private string TryGetValue(DbConnectionStringBuilder dbConnectionStringBuilder, string keyword)
        {
            if (dbConnectionStringBuilder.ContainsKey(keyword))
            {
                return dbConnectionStringBuilder[keyword].ToString();
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
